/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MonthReportView.java
 *
 * Created on Mar 9, 2012, 4:31:34 PM
 */
package ui.report;

import com.nanosl.lib.date.JXDatePicker;
import com.nanosl.lib.log.Loggings;
import com.nanosl.lib.print.JRViewer;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import util.SuperView;

/**
 *
 * @author T
 */
public class MonthReportView extends SuperView {

    private static MonthReportView instance = new MonthReportView();

    public static void display() {
        instance.setVisible(true);
    }

    /**
     * Creates new form MonthReportView
     */
    public MonthReportView(java.awt.Frame parent, boolean modal) {
        this();
    }

    public MonthReportView() {
        super();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        datePicker = new JXDatePicker();
        jLabel1 = new javax.swing.JLabel();
        reportButton = new javax.swing.JButton();
        halfCheckBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        datePicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                datePickerActionPerformed(evt);
            }
        });

        jLabel1.setText("Month:");

        reportButton.setText("Report");
        reportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reportButtonActionPerformed(evt);
            }
        });

        halfCheckBox.setText("Half");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(reportButton)
                .addGap(18, 18, 18)
                .addComponent(halfCheckBox)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reportButton)
                    .addComponent(halfCheckBox))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void datePickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_datePickerActionPerformed
}//GEN-LAST:event_datePickerActionPerformed

    private void reportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reportButtonActionPerformed
        final HashMap<String, Object> params = new HashMap<String, Object>();
        Date date = datePicker.getDate();
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.set(Calendar.DATE, 1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date);
        c2.set(Calendar.DATE, c2.getActualMaximum(Calendar.DATE));
        Calendar c3 = Calendar.getInstance();
        c3.setTime(date);
        c3.set(Calendar.DATE, 1);
        c3.set(Calendar.MONTH, c2.get(Calendar.MONTH) + 1);

        params.put("d1", yyyy_MM_dd.format(c1.getTime()));
        params.put("d2", yyyy_MM_dd.format(c2.getTime()));
        params.put("d3", yyyy_MM_dd.format(c3.getTime()));


        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    String reportSource = "src/rpt/report1.jrxml";
                    if (halfCheckBox.isSelected()) {
                        reportSource = "src/rpt/monthReportA4.jrxml";
                    }
                    JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
                    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, m.getConnection());
                    new JRViewer(jasperPrint, "Month");
                } catch (Exception ex) {
                    Loggings.logError(MonthReportView.class.getName(), ex);
                }
            }
        });
    }//GEN-LAST:event_reportButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MonthReportView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MonthReportView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MonthReportView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MonthReportView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MonthReportView dialog = new MonthReportView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXDatePicker datePicker;
    private javax.swing.JCheckBox halfCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton reportButton;
    // End of variables declaration//GEN-END:variables
}
